// file: main.ts

import { Inventory } from '../model/inventory';
import { IItem } from '../model/inventory.types';
export const testBackpack= () => {
    // --- 1. 定义你的游戏物品数据库 ---
    const itemDB: Record<string, IItem> = {
        '1': { id: '1', name: '石头', maxStack: 64 },
        '2': { id: '2', name: '木头', maxStack: 64 },
        '3': { id: '3', name: '钻石剑', maxStack: 1 },
    };

    // --- 2. 创建一个背包实例 ---
    const playerInventory = new Inventory(27); // 创建一个27格的背包

    // --- 3. 订阅背包事件，以便在数据变化时更新UI ---
    playerInventory.events.on('slot_updated', ({ index, slot }) => {
        
        console.log(
            `UI更新: 槽位 ${index} 变化为 ->`,
            slot.item ? `${slot.item.name} x${slot.quantity}` : '空',
        );
    });

    playerInventory.events.on('refreshed', ({ slots }) => {
        console.log('UI更新: 整个背包刷新');
        slots.forEach((slot, index) => {
            if (slot.item) {
                console.log(
                    `  槽位 ${index}: ${slot.item.name} x${slot.quantity}`,
                );
            }
        });
    });

    // --- 4. 与背包进行交互 ---
    console.log('--- 游戏开始，向背包添加物品 ---');
    let remainingStones = playerInventory.addItem(itemDB['1'], 70); // 添加70个石头
    console.log(`添加70个石头后，剩余: ${remainingStones}`); // 应该剩余 6 (64 + 6)

    let remainingWood = playerInventory.addItem(itemDB['2'], 30); // 添加30个木头
    console.log(`添加30个木头后，剩余: ${remainingWood}`);

    let remainingSword = playerInventory.addItem(itemDB['3'], 1); // 添加1把钻石剑
    console.log(`添加1把钻石剑后，剩余: ${remainingSword}`);

    console.log('\n--- 玩家整理背包：将石头合并 ---');
    // 假设UI检测到玩家将槽位1的物品拖到了槽位0
    playerInventory.moveItem(1, 0);

    console.log('\n--- 玩家再次整理背包：交换木头和剑的位置 ---');
    playerInventory.moveItem(2, 3);

    console.log('\n--- 玩家使用了10个木头 ---');
    playerInventory.removeItem(3, 10); // 假设木头现在在槽位3

    console.log('\n--- 当前背包最终状态 ---');
    playerInventory.forceRefresh();
};
